import {getLoginUser} from "../../../service/user";
import {showToastError, showToastSuccess} from "../../../utils/tool";
import {simplePost} from "../../../utils/request";

const app = getApp()

Page({
    data: {
        nav:{
            title:"编辑资料"
        },
        user:null,
    },
    onLoad: function (options) {
        (async () => {
            const user = await getLoginUser()
            this.setData({
                user,
                options: options||{}
            })
        })()
    },
    avatarChange(e){
      this.setData({
          [`user.avatar`]: e.detail.value
      })
    },
    submit(e){
        const {user} = this.data
        const form = e.detail.value
        form.avatar = user.avatar
        if (!form.avatar){
            showToastError("请上传头像")
            return;
        }
        if (!form.nickname){
            showToastError("请填写昵称")
            return;
        }
        simplePost("/user/update", form).then(r => {
            // 更新用户信息
            app.globalData.user = r.info
            showToastSuccess("保存成功",{
                timeout:() => {
                    wx.navigateBack()
                }
            })
        })
    }
});
